<?php
/**
 * Notes:
 * Created by PhpStorm.
 * Author: TS
 * Date: 2019/10/11
 * Time: 9:06
 */

namespace app\model;


use think\Model;

class FriendPush extends Model
{

    //status : 0 => 失效  1 => 待处理  2 => 已同意  3 => 已拒绝



    public function getUserPush($id){
        return self::join('users','friend_push.target = users.user_id')
            ->where(['friend_push.user_id' => $id])
            ->field(['friend_push_id,friend_push.user_id,users.user_header,user_sex,user_name,user_birthday,user_address,content,type,status as now_status'])
            ->order('friend_push_id','desc')
            ->select();
    }
    public function getById($push_id){
        return self::get($push_id);
    }
    public function check($push_id){
        return self::where(['friend_push_id' => $push_id])
            ->where(['status'=> 1 ])
            ->count();
    }
    public function del($push_id){
        return self::get($push_id)->delete();
    }
    public function checkPushAndUser($user_id,$target){
        return self::get([
            'user_id' => $user_id,
            'target' => $target,
            'status' => 1
        ]);
    }
    public function add($data){
        return self::create($data);
    }
    public function edit($id,$data){
        return self::allowField(true)->save($data,["friend_push_id" => $id]);
    }
    public function getStatus($value){
        $status = [ 0 => "已失效", 1 => "待处理", 2 => "已同意", 3 => "已拒绝"];
        return $status[$value];
    }
    public function checkWait($push_id,$user_id){
        return self::where([
            "friend_push_id" => $push_id,
            "user_id" => $user_id,
            "status" => 1
        ])->count();
    }
    public function getNowStatusAttr($value){
        $status = [ 0 => "已失效", 1 => "待处理", 2 => "已同意", 3 => "已拒绝"];
        return $status[$value];
    }

}